﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CodeCamp.Model;
using CodeCamp.EFDAL;

namespace CodeCamp.Test.EFDAL
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class CodeCampEFDALTests : TestBase
    {
        public CodeCampEFDALTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void SetUpDatabase()
        {
            try
            {
                using (CodeCampDbContext dbContext = new CodeCampDbContext())
                {
                    Assert.IsNotNull(dbContext);
                    int eventCount = dbContext.Events.Count();
                    TestContext.WriteLine("SetUpDatabase - eventCount = {0}", eventCount);
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                ReportException(ex);
                throw;
            }
            catch (System.Data.DataException ex)
            {
                ReportException(ex);
                throw;
            }
        }


        private static class Tags
        {
            internal const string CC_2012FT = "2012FT";
            internal const string CC_2012FT_Desc = "Code Camp Boston Fall 2012 Test";
        }


        [TestMethod]
        public void AddCC2012FallTestIfNotFound()
        {
            try
            {
                using (CodeCampDbContext dbContext = new CodeCampDbContext())
                {
                    Assert.IsNotNull(dbContext);
                    int eventCount = dbContext.Events.Count();
                    TestContext.WriteLine("SetUpDatabase - eventCount = {0}", eventCount);

                    Event ccEvent = null;
                    if (eventCount > 0)
                    {
                        ccEvent = dbContext.Events.Where(e => e.ShortName == "2012FT").FirstOrDefault();
                    }
                    if (ccEvent != null)
                    {
                        TestContext.WriteLine("Found existing event ({0}): \"{1}\": \"{2}\"", ccEvent.Id, ccEvent.ShortName, ccEvent.DisplayName);
                        Assert.AreEqual(Tags.CC_2012FT, ccEvent.ShortName);
                    }
                    else
                    {
                        Event newEvent = new Event() { ShortName = Tags.CC_2012FT, DisplayName = Tags.CC_2012FT_Desc };
                        dbContext.Events.Add(newEvent);
                        dbContext.SaveChanges();
                        ccEvent = dbContext.Events.Where(e => e.ShortName == "2012FT").FirstOrDefault();
                        Assert.IsNotNull(ccEvent);
                        TestContext.WriteLine("Found created event ({0}): \"{1}\": \"{2}\"", ccEvent.Id, ccEvent.ShortName, ccEvent.DisplayName);
                        Assert.AreEqual(Tags.CC_2012FT, ccEvent.ShortName);
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                ReportException(ex);
                throw;
            }
            catch (System.Data.DataException ex)
            {
                ReportException(ex);
                throw;
            }
        }
    }
}
